<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入用户手机号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="提现账号" prop="accountNo">
        <el-input v-model="queryParams.accountNo" placeholder="请输入提现账号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="账户名称" prop="accountName">
        <el-input v-model="queryParams.accountName" placeholder="请输入账户名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.APPROVAL_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item label="审核时间" prop="approveTime">
        <el-date-picker v-model="queryParams.approveTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                         v-hasPermi="['system:user-extract-cash-info:create']">新增
              </el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['system:user-extract-cash-info:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="订单编号" align="center" prop="orderNo"/>
      <el-table-column label="手机号" align="center" prop="phone"/>
      <el-table-column label="提现类型" align="center" prop="type">
        <template v-slot="scope">
          <div v-if="scope.row.type === 'alipay'">支付宝</div>
          <div v-if="scope.row.type === 'bank'">银行卡</div>
        </template>
      </el-table-column>
      <el-table-column label="提现金额" align="center" prop="amount"/>
      <el-table-column label="提现账号" align="center" prop="accountNo"/>
      <el-table-column label="账户名称" align="center" prop="accountName"/>
      <el-table-column label="开户行" align="center" prop="openBank"/>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.APPROVAL_TYPE" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="approveName"/>
      <el-table-column label="描述" align="center" prop="remark"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">-->
      <!--        <template v-slot="scope">-->
      <!--          <span>{{ parseTime(scope.row.updateTime) }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="审核时间" align="center" prop="approveTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.approveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button v-if="scope.row.status == 0" size="mini" type="text" icon="el-icon-edit"
                     @click="auditYes(scope.row)"
                     v-hasPermi="['system:user-extract-cash-info:approve']">通过
          </el-button>
          <el-button v-if="scope.row.status == 0" size="mini" type="text" icon="el-icon-edit"
                     @click="auditNo(scope.row)"
                     v-hasPermi="['system:user-extract-cash-info:approve']">驳回
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="openDetail(scope.row)"
                     v-hasPermi="['system:order-info:getByOrderNo']">详情
          </el-button>
          <!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
                               v-hasPermi="['system:user-extract-cash-info:update']">修改
                    </el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                               v-hasPermi="['system:user-extract-cash-info:delete']">删除
                    </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <UserExtractCashInfoForm ref="formRef" @success="getList"/>

    <!-- 详情弹窗 -->
    <el-dialog :title="`订单详情`" :visible.sync="detailOpen" width="60%">
      <el-form label-width="120px" v-if="currentDetail">
        <el-divider>提现信息</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="提现ID:">{{ currentDetail.row?.id}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提现时间:">{{ parseTime(currentDetail.row?.createTime) }}</el-form-item>
          </el-col>

<!--          <el-col :span="12">-->
<!--            <el-form-item label="提现类型">{{ currentDetail.type === 'alipay' ? '支付宝' : '银行卡' }}</el-form-item>-->
<!--          </el-col>-->
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户手机号:">{{ currentDetail.row?.phone}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单编号:">{{ currentDetail.row?.orderNo}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="提现金额:">￥{{ currentDetail.row?.amount }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账户名称:">{{ currentDetail.row?.accountName }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="开户行:">{{ currentDetail.row?.openBank }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提现账号:">{{ currentDetail.row?.accountNo }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="状态:">
              <dict-tag :type="DICT_TYPE.APPROVAL_TYPE" :value="currentDetail.row?.status"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注:">{{ currentDetail.row?.remark }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="审核时间:">{{ parseTime(currentDetail.row?.approveTime) }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人:">{{ currentDetail.row?.approveName }}</el-form-item>
          </el-col>
        </el-row>

        <el-divider>订单信息</el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="回收类型:">{{ currentDetail.orderInfoDO?.goldType === '0' ? '金条' : '金饰' }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计价克重:">{{ currentDetail.orderExpectPriceInfoDO?.realGoldWeight }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="确认价格时间:">{{ parseTime(currentDetail.orderExpectPriceInfoDO?.confirmTime) }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="确认金价:">{{ currentDetail.orderInfoDO?.goldPrice }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="回收总价:">{{ currentDetail.orderExpectPriceInfoDO?.settlementAmount+currentDetail.orderExpectPriceInfoDO?.serviceFee+currentDetail.orderExpectPriceInfoDO?.expressFee }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务费费率:">{{ currentDetail.orderInfoDO?.serviceFee }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="服务费:">{{ currentDetail.orderExpectPriceInfoDO?.serviceFee }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物流费:">{{ currentDetail.orderExpectPriceInfoDO?.expressFee }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="15">
            <el-form-item label="结算金额:">{{ currentDetail.orderExpectPriceInfoDO?.settlementAmount }}</el-form-item>
          </el-col>
        </el-row>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import * as UserExtractCashInfoApi from '@/api/system/userextractcashinfo';
import UserExtractCashInfoForm from './UserExtractCashInfoForm.vue';

export default {
  name: "UserExtractCashInfo",
  components: {
    UserExtractCashInfoForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户提现列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        uid: null,
        status: null,
        createTime: [],
        approveTime: [],
        accountName: null,
        accountNo: null,
      },
      currentDetail: {
        orderCheckInfoDO: null,
        orderExpectPriceInfoDO: null,
        orderExpressInfoDO: null,
        orderInfoDO: null,
        row: null,
      }, // 初始化为空对象，匹配接口返回的数据结构
      detailOpen: false, // 控制详情弹窗的显示/隐藏
      detailLoading: false, // 详情加载状态
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await UserExtractCashInfoApi.getUserExtractCashInfoPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除用户提现编号为"' + id + '"的数据项?')
      try {
        await UserExtractCashInfoApi.deleteUserExtractCashInfo(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有用户提现数据项?');
      try {
        this.exportLoading = true;
        const data = await UserExtractCashInfoApi.exportUserExtractCashInfoExcel(this.queryParams);
        this.$modal.msgSuccess("提交下载申请成功，稍后在下载记录中查看");
        // this.$download.excel(data, '用户提现.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },

    /** 审核通过 */
    async auditYes(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认审核通过用户提现编号为"' + id + '"的数据项?')
      try {
        await UserExtractCashInfoApi.approve({"id": id, "status": 1, "remark": "审核通过"});
        await this.getList();
        this.$modal.msgSuccess("审核通过成功");
      } catch {
      }
    },

    /** 审核驳回 */
    async auditNo(row) {
      const id = row.id;
      const {value} = await this.$prompt('请输入驳回原因', '审核驳回', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^(?!\s*$).{1,100}$/,
        inputErrorMessage: '备注不能为空且长度不能超过100个字符'
      });
      await this.$modal.confirm('是否确认驳回通过用户提现编号为"' + id + '"的数据项?')
      try {
        await UserExtractCashInfoApi.approve({"id": id, "status": 2, "remark": value});
        await this.getList();
        this.$modal.msgSuccess("驳回成功");
      } catch {
      }
    },
    /** 查看详情 */
    async openDetail(row) {
      try {
        const orderNo = row.orderNo;
        this.detailLoading = true;
        const res = await UserExtractCashInfoApi.getByOrderNo(orderNo);
        this.currentDetail = res.data;
        this.currentDetail.row = row;
        console.log('=====',this.currentDetail);
        this.detailOpen = true;
      } catch (error) {
        this.$modal.msgError("获取详情失败");
      } finally {
        this.detailLoading = false;
      }
    },

  }
};
</script>
